class Solution {
public:
    vector<int> sortArray(vector<int>& nums) {
        srand(time(nullptr)); // 随机数种子
        qsort(nums, 0, nums.size() - 1);
        return nums;
    }
 
    void qsort(vector<int>& nums, int begin, int end)
    {
        if(begin >= end)
            return;
 
        int key = nums[rand() % (end - begin + 1) + begin]; // 随机选key
        // rand() % (r - l + 1)是[0, n-1] 再加偏移量begin就是要排序的区间
        int i = begin, left = begin - 1, right = end + 1;
        while(i < right)
        {
            if(nums[i] < key)
                swap(nums[++left], nums[i++]);
            else if(nums[i] == key)
                ++i;
            else // >key
                swap(nums[--right], nums[i]);
        }
        // [begin, left] [left + 1, right - 1] [right, end]
        qsort(nums, begin, left);
        qsort(nums, right, end);
    }
};